import com.sun.org.apache.bcel.internal.generic.NEW;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Classname ClientHandler
 * @Description TODO
 * @Date 2020/8/18 12:42
 * @Created by pc
 */
@ChannelHandler.Sharable
public class ClientHandler extends ChannelInboundHandlerAdapter {

    private ChannelHandlerContext ctxData = null;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        ctxData = ctx;
        System.out.println("connetted");
//        ctx.channel().writeAndFlush("hi, nice to see you");
        System.out.println("======handler active thread id is: " + Thread.currentThread().getId() + "--" + "in bound " + ctx.channel().toString() + ctx.channel().remoteAddress());
        System.out.println();
        ctx.writeAndFlush("hi, nice to see you");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("======handler read thread id is: " + Thread.currentThread().getId() + "--" + "in bound " + ctx.channel().toString() + ctx.channel().remoteAddress());
        System.out.println(msg.toString() + "from server:" + ctx.channel().remoteAddress());
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//        String msg = "my data is here: " + new Date();
//        ctx.writeAndFlush(msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

    public ChannelHandlerContext getCtxData() {
        return ctxData;
    }

    public void setCtxData(ChannelHandlerContext ctxData) {
        this.ctxData = ctxData;
    }
}
